<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application;

use Application\Model\AuthStorage;
use Application\Model\User;
use Application\Model\UserTable;
use Zend\Authentication\Adapter\DbTable\CallbackCheckAdapter;
use Zend\Authentication\AuthenticationService;
use Zend\Crypt\Password\Bcrypt;
use Zend\EventManager\StaticEventManager;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
    const BACKEND_AUTH_NAMESPACE = 'Zend_Auth_Backend';
    const FRONTEND_AUTH_NAMESPACE = 'Zend_Auth_Frontend';
    protected $authService;

    public function onBootstrap(MvcEvent $e)
    {
        $eventManager = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);

        $e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractController', 'dispatch', function ($e) {
            $controller = $e->getTarget();
            $controllerClass = get_class($controller);
            $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
            $config = $e->getApplication()->getServiceManager()->get('config');
            if (isset($config['module_layouts'][$moduleNamespace])) {
                $controller->layout($config['module_layouts'][$moduleNamespace]);
            }
        }, 100);
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                    'Cf'        => __DIR__ . '/../../library/Cf',
                ),
            ),
        );
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'AuthStorageFront' => function ($sm) {
                        return new AuthStorage($this::FRONTEND_AUTH_NAMESPACE);
                    },
                'AuthStorageBack' => function ($sm) {
                        return new AuthStorage($this::BACKEND_AUTH_NAMESPACE);
                    },
                'AuthFront' => function ($sm) {
                        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                        $dbTableAuthAdapter = new CallbackCheckAdapter($dbAdapter, 'user', 'email', 'password', function ($credentialColumn, $credential) {
                            $bCrypt = new Bcrypt();
                            return $bCrypt->verify($credential, $credentialColumn);
                        });

                        $authService = new AuthenticationService();
                        $authService->setAdapter($dbTableAuthAdapter);
                        $authService->setStorage($sm->get("AuthStorageFront"));

                        return $authService;
                    },
                'AuthBack' => function ($sm) {
                        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                        $dbTableAuthAdapter = new CallbackCheckAdapter($dbAdapter, 'user', 'email', 'password', function ($credentialColumn, $credential) {
                            $bCrypt = new Bcrypt();
                            return $bCrypt->verify($credential, $credentialColumn);
                        });

                        $authService = new AuthenticationService();
                        $authService->setAdapter($dbTableAuthAdapter);
                        $authService->setStorage($sm->get("AuthStorageBack"));

                        return $authService;
                    },
            ),
        );
    }

}
